package model

import (
	"gorm.io/gorm"
	"user_srv/global"
)

type Book struct {
	gorm.Model
	Name    string `gorm:"column:name;type:varchar(50);not null;comment:名称"`
	Content string `gorm:"column:content;type:varchar(200);not null;comment:介绍"`
	Types   string `gorm:"column:types;type:varchar(20);not null;comment:分类"`
	Author  string `gorm:"column:author;type:varchar(30);not null;comment:作者"`
	Hot     int    `gorm:"column:hot;type:int;not null;comment:热度"`
	Status  int    `gorm:"column:status;type:int;not null;comment:状态"`
}

// 通过查看漫画
func GetBooksByName(id int) (b *Book, err error) {
	err = global.DB.Where("id = ?", id).Find(&b).Error
	return
}

// 查看漫画列表
func BookList(name string, page, limit int) (b []Book, err error) {
	offset := (page - 1) * limit
	if name != "" {
		err = global.DB.Where("name like ?", "%"+name+"%").Offset(offset).Limit(limit).Find(&b).Error
	} else {
		err = global.DB.Offset(offset).Limit(limit).Find(&b).Error
	}
	return b, err
}

// 查看漫画列表
func BookLists(types string, page, limit int) (b []Book, err error) {
	offset := (page - 1) * limit
	err = global.DB.Where("types = ?", types).Offset(offset).Limit(limit).Find(&b).Error

	return b, err
}

func (b *Book) Create() error {
	return global.DB.Create(&b).Error
}
